<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //  13.用函数封装一个数组的unshift方法
        // 在数组的前面添加一个或多个元素
        var arr = ["a", "b", "c"];
        // var a = 10;
        // arr[0] = a; //arr=[10,"b","c"]
        // arr[arr.length] = arr[2]; //arr=["a","b","c","c"]
        // arr[2] = arr[1]; //arr=["a","b","b","c"]
        // arr[1] = arr[0]; //arr=["a","a","b","c"]
        // arr[0] = a; //arr=[10,"a","b","c"]

        // for(var i = arr.length;i > 0 ;i--){
        //     console.log(arr[i]);
        //     arr[i] = arr[i - 1];
        // }
        // arr[0] = a;

        var arr1 = [10,20,30,40];

        for(var k = arr1.length - 1;k >= 0;k--){
            for(var i = arr.length;i > 0 ;i--){
                arr[i] = arr[i - 1];
            }
            arr[0] = arr1[k];
        }

        function unshift2(array) {
            // arguments
            for (var k = arguments.length - 1; k >= 1; k--) {
                for (var i = array.length; i > 0; i--) {
                    array[i] = array[i - 1];
                }
                array[0] = arguments[k];
            }
        }
        // unshift2(arr,10)
        // console.log(arr);

        Array.prototype.unshift3 = function () {
            for (var k = arguments.length - 1; k >= 0; k--) {
                for (var i = this.length; i > 0; i--) {
                    this[i] = this[i - 1];
                }
                this[0] = arguments[k];
            }
        }




        //  14.用函数封装一个字符串的substr方法
        // 字符串截取 截取从n开始m个字符
        var str = "qwertyuiop";
        var n = 2;
        var m = 5;
        // var str1 = str[2] + str[3] + str[4] + str[5] + str[6];
        // console.log("str1:",str1);

        // var str1 = "";
        // for (var i = n; i < n + m; i++) {
        //     str1 += str[i]
        // }

        function substr2(string,n,m){
            if (!m) m = string.length-n;
            var str1 = "";
            for (var i = n; i < n + m; i++) {
                str1 += string[i]
            }
            return str1;
        }
        // var s = substr2(str,2);
        // console.log(s);

        String.prototype.substr3 = function(n,m){
            if (!m) m = this.length-n;
            var str1 = "";
            for (var i = n; i < n + m; i++) {
                str1 += this[i]
            }
            return str1;
        }




        //  15.用函数封装一个数组的join方法
        // 将数组转换成字符串
        arr = [12, 23, 12];

        // var str = "" + arr[0] + "," + arr[1] + "," + arr[2];

        // var str = ""
        // for(var i = 0;i < arr.length - 1;i++){
        //     str += arr[i] + ",";
        //     console.log(i); //0  1
        // }
        // // console.log(i); //2
        // str += arr[i]

        function join2(array, b = ",") {
            console.log(b);
            var str = ""
            for (var i = 0; i < arr.length - 1; i++) {
                str += arr[i] + b;
            }
            str += arr[i];
            return str;
        }
        // var s = join2(arr,"-");
        // console.log(s);   

        Array.prototype.join3 = function (b = ",") {
            var str = ""
            for (var i = 0; i < this.length - 1; i++) {
                str += this[i] + b;
            }
            str += this[i];
            return str;
        }
        // arr.join3();



       
        // 获取 一个元素 在 数组 中 第一次出现的位置 如果没有出现(都不相等)则返回-1
        var str = "bbbbbb";
        var arr = ["aaa","aaa","bbb","ccc","bbb","ccc"];
        Array.prototype.indexOf3 = function(str){
            for(var i = 0;i < this.length;i++){
                // if(str == this[i]){
                //     return i;
                // }
                if(str == this[i]) return i;
            }
            // console.log("===");
            return -1;
        }

        var b = arr.indexOf3("bbb");
        console.log(b);


        function indexOf2(array,str,b = 0){
            // b = b ? b : 0;
            for(var i = b;i < array.length;i++){
                if(str == array[i]) return i;
            }
            // console.log("===");
            return -1;
        }
        var c = indexOf2(arr,"bbb");
        console.log(c);



        var cont = 0;
        for(var i = 0;i < arr.length;i++){
            if(str == arr[i]){
                cont++;
                console.log(i); //2
                break;
            }
        }
        if(cont == 0){
            console.log(-1);
        }


        // 判断str与 数组中每一个值 从前往后
        if(arr[0] == str){
            console.log(0);
        }else if(arr[1] == str){
            console.log(1);
        }else if(arr[2] == str){
            console.log(2);
        }else if(arr[3] == str){
            console.log(3);
        }else if(arr[4] == str){
            console.log(4);
        }else if(arr[5] == str){
            console.log(5);
        }else{
            console.log(-1);
        }

        var a = arr[0] == str ? "0" : arr[1] == str ? 1 : arr[2] == str ? 2 : arr[3] == str ? 3 : arr[4] == str ? 4 : arr[5] == str ? 5 : -1;
        console.log(a);

        


    </script>
</body>

</html>